package com.realbrand.net.realbrandpay.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.realbrand.net.realbrandpay.R;

/**
 * Created by Administrator on 2018/1/12.
 */

public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {
    private View moreView;
    private Context mContext;
    private boolean isLoading = false;
    private IloadListener iloadListener;//数据加载接口

    public LoadMoreListView(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    private void initView() {
        //底部加载更多的信息提示布局
        moreView = LayoutInflater.from(mContext).inflate(R.layout.footlistview_layout, null);
        //默认隐藏正在加载的提示信息布局
        moreView.findViewById(R.id.load_layout).setVisibility(View.GONE);
        //吧提示布局添加到ListView中
        this.addFooterView(moreView);
        this.setOnScrollListener(this);

    }

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        //判断是否滑到最后一条
        if (view.getLastVisiblePosition() == view.getCount() - 1
                && scrollState == SCROLL_STATE_IDLE) {
            if (!isLoading) {
                isLoading = true;
                moreView.findViewById(R.id.load_layout).setVisibility(View.VISIBLE);
                iloadListener.onLoad();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (isLoading) {
            moreView.findViewById(R.id.load_layout).setVisibility(View.VISIBLE);
        }
    }

    /**
     * 当数据加载完成时调用此方法
     */
    public void loadCompLete() {
        isLoading = false;
        moreView.findViewById(R.id.load_layout).setVisibility(View.GONE);
    }

    public void setInretface(IloadListener iloadListener) {
        this.iloadListener = iloadListener;
    }

    public interface IloadListener {
        public void onLoad();
    }
}
